﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using ProduceBasicData.Api.Application.Command.IncomingMaterialsManage;
using ProduceBasicData.Domain.IncomingMaterialsManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;
using ProduceBasicData.Api.Services; //编码

namespace ProduceBasicData.Api.Application.Handler.IncomingMaterialsManage
{
    /// <summary>
    /// 来料检验录入处理类
    /// </summary>
    public class CreateIncomingInspectionHandler : IRequestHandler<CreateIncomingInspectionCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<IncomingInspection> _incomingInspectionRepository;
        private readonly IMapper _mapper;
        private readonly ICodeRuleService codeRuleService;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="incomingInspectionRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="codeRuleService"></param>
        public CreateIncomingInspectionHandler(IBaseRespository<IncomingInspection> incomingInspectionRepository, IMapper mapper,
            ICodeRuleService codeRuleService)
        {
            _incomingInspectionRepository = incomingInspectionRepository;
            _mapper = mapper;
            this.codeRuleService = codeRuleService;
        }
        /// <summary>
        /// 处理来料检验录入请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(CreateIncomingInspectionCommand request, CancellationToken cancellationToken)
        {
            try
            {
                //将命令转换为领域模型
                var incomingInspection = _mapper.Map<IncomingInspection>(request);
                //判断来料检验是否存在
                var incomingInspectionExist = await _incomingInspectionRepository.GetAll().AnyAsync(x => x.InspectionNo == incomingInspection.InspectionNo);
                if (incomingInspectionExist != false)
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "来料检验已存在"
                    };
                }
                //编码
                if (string.IsNullOrEmpty(incomingInspection.InspectionNo))
                {
                    var code = await codeRuleService.AutoGenerateCodeAsync("ProduceBasicData.Domain.IncomingMaterialsManage.IncomingInspection");
                    if (!string.IsNullOrEmpty(code))
                    {
                        incomingInspection.InspectionNo = code;
                    }
                    else
                    {
                        incomingInspection.InspectionNo = $"LL{DateTime.Now:yyyyMMddHHmmss}";
                    }
                }

                //保存来料检验
                var result = await _incomingInspectionRepository.AddAsync(incomingInspection);
                if (result > 0)
                {
                    return new ApiResult<int>
                    {
                        Data = result,
                        Code = ApiEnum.Success,
                        Msg = "来料检验录入成功"
                    };
                }
                else
                {
                    return new ApiResult<int>
                    {
                        Code = ApiEnum.Failed,
                        Msg = "来料检验录入失败"
                    };
                }
            }
            catch (Exception ex)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.Failed,
                    Msg = ex.Message
                };
            }
        }
    }
}
